package com.puboot.module.blog.controller;

import com.puboot.common.util.*;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/cement/api")
@AllArgsConstructor
public class CementController {

    private final HttpServletRequest httpServletRequest;
    private final CementService cementService;
    private final CementDetailService cementDetailService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(Cement cement) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> ncvs = new ArrayList<>();
            if (StringUtils.isNotBlank(cement.getNcv())) {
                ncvs = Arrays.asList(cement.getNcv().split(","));
            }
            List<String> fcs = new ArrayList<>();
            if (StringUtils.isNotBlank(cement.getFc())) {
                fcs = Arrays.asList(cement.getFc().split(","));
            }
            List<String> ccs = new ArrayList<>();
            if (StringUtils.isNotBlank(cement.getCc())) {
                ccs = Arrays.asList(cement.getCc().split(","));
            }
            List<String> ofs = new ArrayList<>();
            if (StringUtils.isNotBlank(cement.getOf())) {
                ofs = Arrays.asList(cement.getOf().split(","));
            }

            Double eBurn = SpinUtil.getEBurn(ncvs, fcs, ccs, ofs);
            Double eProcess = CementUtil.getEProcess(cement.getQ(), cement.getFr1(), cement.getFr10(), cement.getFr2(), cement.getFr20());
            Double ePurchaseElectric = SpinUtil.getEPurchaseElectric(cement.getAdPurchaseElectric(), cement.getEfPurchaseElectric());
            Double ePurchaseHeat = SpinUtil.getEPurchaseHeatOrEOutputHeat(cement.getAdPurchaseHeat());
            Double eOutputHeat = SpinUtil.getEPurchaseHeatOrEOutputHeat(cement.getAdOutInputHeat());
            Double eOutputElectric = SpinUtil.getEOutputElectric(cement.getAdOutInputElectric(), cement.getEfOutInputElectric());
            Double result = CementUtil.getE(eBurn, eProcess, ePurchaseElectric,
                    ePurchaseHeat, eOutputElectric, eOutputHeat);
            cement.setCustomerId(customer.getId());
            cement.setCreateTime(new Date());
            cement.setEBurn(GasDCUtil.round(eBurn, 2));
            cement.setEProcess(GasDCUtil.round(eProcess, 2));
            cement.setEPurchaseElectric(GasDCUtil.round(ePurchaseElectric, 2));
            cement.setEPurchaseHeat(GasDCUtil.round(ePurchaseHeat, 2));
            cement.setEOutputHeat(GasDCUtil.round(eOutputHeat, 2));
            cement.setEOutputElectric(GasDCUtil.round(eOutputElectric, 2));
            cement.setState(Constants.STATE_INVAILD);
            cement.setResult(GasDCUtil.getThousandth(result));
            cementService.save(cement);
            for (int i = 0; i < ncvs.size(); i++) {
                CementDetail cementDetail = new CementDetail();
                cementDetail.setNcv(Double.valueOf(ncvs.get(i)));
                cementDetail.setFc(Double.valueOf(fcs.get(i)));
                cementDetail.setCc(Double.valueOf(ccs.get(i)));
                cementDetail.setOf(Double.valueOf(ofs.get(i)));
                cementDetail.setCementId(cement.getId());
                cementDetailService.save(cementDetail);
            }
            return ResultUtil.success("计算成功", cement);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
